<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料主表配置ID">
              <j-popup
                placeholder="请选择物料主表配置ID"
                v-model="queryParam.cfgid"
                code="bom"
                org-fields="partno"
                dest-fields="cfgid"
                :field="getPopupField('cfgid')"
                :multi="true" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="送检单号">
              <a-input placeholder="请输入送检单号" v-model="queryParam.checkno"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="送检日期">
                <j-date placeholder="请选择送检日期" v-model="queryParam.checkdate"></j-date>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('qis-来料检测送检单数据表')">导出</a-button>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal"
                     @handleSuperQuery="handleSuperQuery"></j-super-query>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="recId"
        class="j-table-force-nowrap"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type:'radio'}"
        :customRow="clickThenSelect"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            :preview="record.id"
            height="25px"
            alt=""
            style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a v-if="record.result==='3'" @click="createCheckSheet(record)">生成检验单</a>
          <a v-else @click="openSheet(record)">检验</a>
          <!--          <a-dropdown>-->
          <!--            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>-->
          <!--            <a-menu slot="overlay">-->
          <!--              <a-menu-item>-->
          <!--                -->
          <!--              </a-menu-item>-->
          <!--            </a-menu>-->
          <!--          </a-dropdown>-->
        </span>

      </a-table>
    </div>

    <!--    <a-tabs defaultActiveKey="1">-->
    <!--      <a-tab-pane tab="qis-来料检测结果明细表" key="1" >-->
    <!--        <QisMatdetailValueList :mainId="selectedMainId" :cfgid="cfgid"/>-->
    <!--      </a-tab-pane>-->
    <!--      <a-tab-pane tab="qis-来料检测送检单批次表" key="2" forceRender>-->
    <!--        <QisMatdetailLotList :mainId="selectedMainId" />-->
    <!--      </a-tab-pane>-->
    <!--    </a-tabs>-->

    <qisMatheadValue-modal ref="modalForm" @ok="modalFormOk"></qisMatheadValue-modal>

    <QisMatheadSheetModal ref="modalSheet" :isSheet="isSheet"></QisMatheadSheetModal>
  </a-card>
</template>

<script>

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import QisMatheadValueModal from './modules/QisMatheadValueModal'
import QisMatheadSheetModal from './modules/QisMatheadSheetModal'
import { getAction, postAction } from '@/api/manage'
import '@/assets/less/TableExpand.less'

export default {
  name: 'QisMatheadValueList',
  mixins: [JeecgListMixin],
  components: {
    QisMatheadSheetModal,
    QisMatheadValueModal
  },
  data() {
    return {
      description: 'qis-来料检测送检单数据表管理页面',
      // 表头
      columns: [
        {
          title: '主键ID',
          align: 'center',
          dataIndex: 'recId'
        },
        {
          title: '物料描述',
          align: 'center',
          dataIndex: 'description'
        },
        {
          title: '送检单号',
          align: 'center',
          dataIndex: 'checklistno'
        },
        {
          title: '送检日期',
          align: 'center',
          dataIndex: 'checkdate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '入库批次',
          align: 'center',
          dataIndex: 'lotno'
        },
        {
          title: '检测类型',
          align: 'center',
          dataIndex: 'checkTyp_dictText'
        },
        {
          title: '当前检验次数',
          align: 'center',
          dataIndex: 'tn'
        },
        {
          title: '报交数',
          align: 'center',
          dataIndex: 'qty'
        },
        {
          title: '检验结果',
          align: 'center',
          dataIndex: 'result'
        },
        {
          title: '供应商',
          align: 'center',
          dataIndex: 'vendor'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '操作者',
          align: 'center',
          dataIndex: 'user'
        },
        {
          title: '更新时间',
          align: 'center',
          dataIndex: 'ctime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/qis/qisMatheadValue/list',
        delete: '/qis/qisMatheadValue/delete',
        createCheckSheet: '/qis/qisMatheadValue/createCheckSheet',
        deleteBatch: '/qis/qisMatheadValue/deleteBatch',
        exportXlsUrl: '/qis/qisMatheadValue/exportXls',
        importExcelUrl: 'qis/qisMatheadValue/importExcel'
      },
      dictOptions: {},
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 5,
        pageSizeOptions: ['5', '10', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      selectedMainId: '',
      cfgid: '',
      superFieldList: [],
      isSheet: false
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    /**
     * 打开检验单页面
     */
    openSheet(record) {
      this.$refs.modalSheet.open(record)
      console.log('打开检验单')
    },
    createCheckSheet(record) {
      this.loading = true
      postAction(this.url.createCheckSheet, record).then((res) => {
        this.$message.success('生成成功!')
        this.loading = false
      })
    },
    initDictConfig() {
    },
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            console.log(record)
            const workId = record.recId + ''

            this.onSelectChange(workId.split(','), [record], record.cfgid)
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows, cfgid) {
      this.cfgid = cfgid
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.onClearSelected()
      var params = this.getQueryParams()// 查询条件
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'recId', text: '主键ID', dictCode: '' })
      fieldList.push({
        type: 'popup',
        value: 'cfgid',
        text: '物料主表配置ID',
        popup: { code: 'bom', field: 'partno', orgFields: 'partno', destFields: 'cfgid' }
      })
      fieldList.push({ type: 'string', value: 'checkno', text: '送检单号', dictCode: '' })
      fieldList.push({ type: 'date', value: 'checkdate', text: '送检日期' })
      fieldList.push({ type: 'BigDecimal', value: 'times', text: '送检次数', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'qty', text: '报交数', dictCode: '' })
      fieldList.push({ type: 'string', value: 'result', text: '状态', dictCode: '' })
      fieldList.push({ type: 'string', value: 'vendor', text: '供应商', dictCode: '' })
      fieldList.push({ type: 'string', value: 'remark', text: '备注', dictCode: '' })
      fieldList.push({ type: 'string', value: 'user', text: '操作者', dictCode: '' })
      fieldList.push({ type: 'date', value: 'ctime', text: '更新时间' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less'
</style>
